//
//  YZPigDeathDetailViewModel.m
//  VET
//
//  Created by YZ on 2018/7/19.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPigDeathDetailViewModel.h"
#import "YZPigDeathDetailModel.h"
#import "YZPigDeathDetailRecordModel.h"
#import "NSDate+Category.h"

@implementation YZPigDeathDetailViewModel

@synthesize dataSource = _dataSource;
@synthesize index = _index;

- (instancetype)init
{
    if(self = [super init]){
        [self config];
    }
    return self;
}

- (void)config
{
    _dataSource = @[[NSMutableArray array],[NSMutableArray array]];
}

- (void)headerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    _index = 0;
    YZWeakSelf(self);
    NSMutableArray *arr = (NSMutableArray *)_dataSource.lastObject;
    [self requestDieOutTrackSuccess:^(NSDictionary *obj) {
        if (((NSArray *)((NSDictionary *)obj[@"data"])[@"list"]).count != 0) {
            weakself.totalPage = [((NSDictionary *)obj[@"data"])[@"lastPage"] integerValue] - 1;
        }else
        {
            weakself.totalPage = 0;
        }
        [arr removeAllObjects];
        for (NSDictionary *dic in (NSArray *)((NSDictionary *)obj[@"data"])[@"list"]) {
            YZPigDeathDetailRecordModel *model = [YZPigDeathDetailRecordModel mj_objectWithKeyValues:dic];
            [arr addObject:model];
        }
        if (successBlock) {
            successBlock(arr);
        }
        NSLog(@"index:%ld,arr:%ld",_index,arr.count);
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)footerRefreshSuccess:(void (^)(id))successBlock fail:(void (^)(NSError *))failBlock
{
    _index++;
    NSMutableArray *arr = (NSMutableArray *)_dataSource.lastObject;
    [self requestDieOutTrackSuccess:^(NSDictionary *obj) {
        for (NSDictionary *dic in (NSArray *)((NSDictionary *)obj[@"data"])[@"list"]) {
            YZPigDeathDetailRecordModel *model = [YZPigDeathDetailRecordModel mj_objectWithKeyValues:dic];
            [arr addObject:model];
        }
        if (successBlock) {
            successBlock(((NSDictionary *)obj[@"data"])[@"list"]);
        }
        NSLog(@"index:%ld,arr:%ld",_index,arr.count);
    } fail:^(NSError *error) {
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)requestDieOutTrackSuccess:(YZNetworkCompletionSuccessBlock)successBlock fail:(YZNetworkCompletionFailBlock)failBlock
{
    self.loading = YES;
    NSDictionary *params = @{@"index":[NSString stringWithFormat:@"%ld",_index],@"size":@"10"};
    [YZNetworkManager getWithApi:[NSString stringWithFormat:@"/api/dieout/track/%ld",self.model.dieOutId] params:params withHeaders:nil success:^(NSDictionary *obj) {
        self.loading = NO;
        if (successBlock) {
            successBlock(obj);
        }
    } fail:^(NSError *error) {
        self.loading = NO;
        if (failBlock) {
            failBlock(error);
        }
    }];
}

- (void)setModel:(YZDieOutModel *)model
{
    _model = model;
    NSMutableArray *arr = (NSMutableArray *)_dataSource.firstObject;
    [arr removeAllObjects];
    YZPigDeathDetailModel *earModel = [YZPigDeathDetailModel new];
    earModel.title = @"耳标号";
    earModel.content = model.dieOutNO;
    
    YZPigDeathDetailModel *typeModel = [YZPigDeathDetailModel new];
    typeModel.title = @"分类";
    typeModel.content = model.dieoutClass.itemValue;
    
    YZPigDeathDetailModel *dateModel = [YZPigDeathDetailModel new];
    dateModel.title = @"日龄 (天)";
    dateModel.content = [NSString stringWithFormat:@"%ld",(NSInteger)ceil((model.dieoutTime - model.birthdate)/(24.0*3600))];
    
    YZPigDeathDetailModel *deathModel = [YZPigDeathDetailModel new];
    deathModel.title = @"死亡日期";
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.dieoutTime];
    deathModel.content = [date localDateString];
    
    YZPigDeathDetailModel *statusModel = [YZPigDeathDetailModel new];
    statusModel.title = @"死前状态";
    statusModel.content = model.dieoutState.itemValue;
    
    [arr addObject:earModel];
    [arr addObject:typeModel];
    [arr addObject:dateModel];
    [arr addObject:deathModel];
    [arr addObject:statusModel];
    
//    YZPigDeathDetailRecordImgModel *imgModel = [YZPigDeathDetailRecordImgModel new];
//    imgModel.img = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1531978715354&di=d530afa6d38b73f1848f5ce5c581409c&imgtype=0&src=http%3A%2F%2Fimgbbs.heiguang.net%2Fforum%2F201411%2F11%2F153204wkvkdkiwkmtlodmd.jpg";
//
//    YZPigDeathDetailRecordModel *recordModel = [YZPigDeathDetailRecordModel new];
//    recordModel.date = @"2018-02-23  13:30";
//    recordModel.type = @"死淘分析";
//    recordModel.desc = @"还没有找到死亡原因";
//    recordModel.imgs = @[imgModel,imgModel,imgModel,imgModel];
}
@end
